#include <cstdlib>
#include <iostream>

using namespace std;

int maxfactor(int i1, int i2);

int main(int argc, char *argv[])
{
    int i1,i2;

    cout<<"Please input two integers, the program will find their max common factor."<<endl;
    cin>>i1>>i2;
    cout<<"Ans:"<<maxfactor(i1,i2)<<endl;
        
    system("PAUSE");
    return EXIT_SUCCESS;
}

// edge conditions of i1, i2 are considered. i1, i2 can be negative and i1 can less than i2.
// i1 must be larger than or equal to i2
int maxfactor(int i1, int i2)
{
    if(i1<0) i1=-i1;
    if(i2<0) i2=-i2; 
    if(i1<i2)return maxfactor(i2,i1);
        
    int r1;
    while((r1=i1%i2)>0){
        i1=i2;
        i2=r1;
    }
    return i2;
}
